<?php


namespace App\Bizs\Mp1;


use App\Exceptions\BizException;
use App\Models\GiveCommentModel;
use App\Models\GiveModel;
use App\Models\UserModel;
use App\Supports\DateSupport;
use Base\BaseBiz;
use Lib\Log;

class CommentBiz extends BaseBiz
{
    /**
     * 发送评论
     * @param $params
     * @param $uid
     * @return array
     * @throws BizException
     */
    public function sendComment($params, $uid)
    {
        if (empty($params['content'])) {
            throw new BizException('请输入评论内容');
        }
        if (empty($params['give_id'])) {
            throw new BizException('请指定送养id');
        }
        $give = (new GiveModel())->getGiveRowById($params['give_id']);
        if (empty($give)) {
            throw new BizException('该贴不存在');
        }
        try {
            $insert = (new GiveCommentModel())->insertComment([
                'uid' => $uid,
                'give_id' => $params['give_id'],
                'content' => $params['content'],
                'create_time' => DateSupport::nowDate()
            ]);

            // 计算评论量
            $update = (new GiveModel())->updateGive([
                'id' => $params['give_id']
            ], [
                'comment_count' => (new GiveCommentModel())->getListCount([
                    'give_id' => $params['give_id']
                ])
            ]);
            return [
                'insert' => $insert,
                'update' => $update
            ];
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            throw new BizException('评论失败');
        }
    }

    /**
     * @param $params
     * @param $uid
     * @return array
     * @throws BizException
     */
    public function deleteComment($params, $uid)
    {
        if (empty($params['id'])) {
            throw new BizException('请指定要删除的评论id');
        }
        $comment = (new GiveCommentModel())->getCommentRowById($params['id']);
        if (empty($comment)) {
            throw new BizException('该评论不存在');
        }
        if ($comment['uid'] != $uid) {
            throw new BizException('不允许删除别人的评论');
        }
        try {
            $update = (new GiveCommentModel())->fakeDeleteComment($params['id'], $uid);
            return [
                'update' => $update
            ];
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            throw new BizException('删除失败');
        }
    }

    /**
     * @param $params
     * @return array
     * @throws BizException
     */
    public function getCommentList($params)
    {
        if (empty($params['give_id'])) {
            throw new BizException('请指定送养id');
        }

        $whereArr = [
            'give_id' => $params['give_id'],
            'is_del' => 0,
            'ORDER' => ["create_time" => "DESC"]
        ];
        $count = (new GiveCommentModel())->getListCount($whereArr);
        if ($count <= 0) {
            return [
                'count' => 0,
                'data' => []
            ];
        }
        $data = (new GiveCommentModel())->getListByPage(
            $whereArr,
            ['id', 'uid', 'give_id', 'content', 'create_time'],
            $params['limit'] ?? 20,
            $params['page'] ?? 1
        );

        $users = (new UserModel())->getUserDirByUid(
            array_column($data, 'uid'),
            ['id', 'nickname', 'icon', 'gender']
        );

        foreach ($data as &$v) {
            $v['user'] = $users[$v['uid']] ?? [];
        }

        return [
            'count' => $count,
            'data' => $data
        ];
    }
}
